package com.jzo2o.customer.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUser;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.WorkerCertificationAuditService;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements WorkerCertificationAuditService {
    @Autowired
    private WorkerCertificationMapper workerCertificationMapper;

    @Override
    public PageResult<WorkerCertificationAuditResDTO> pageQuery(WorkerCertificationAuditPageQueryReqDTO workerCertificationAuditPageQueryReqDTO) {
        PageUtils.parsePageQuery(workerCertificationAuditPageQueryReqDTO, WorkerCertificationAudit.class);
        Integer pageNo = Math.toIntExact(workerCertificationAuditPageQueryReqDTO.getPageNo());
        Integer pageSize = Math.toIntExact(workerCertificationAuditPageQueryReqDTO.getPageSize());
        Page<WorkerCertificationAudit> page = new Page<>(pageNo, pageSize);
        String name = workerCertificationAuditPageQueryReqDTO.getName();
        String idCardNo = workerCertificationAuditPageQueryReqDTO.getIdCardNo();
        Integer auditStatus = workerCertificationAuditPageQueryReqDTO.getAuditStatus();
        Integer certificationStatus = workerCertificationAuditPageQueryReqDTO.getCertificationStatus();
        this.lambdaQuery().like(ObjectUtil.isNotEmpty(name), WorkerCertificationAudit::getName, name)
            .like(ObjectUtil.isNotEmpty(idCardNo), WorkerCertificationAudit::getIdCardNo, idCardNo)
            .eq(ObjectUtil.isNotEmpty(auditStatus), WorkerCertificationAudit::getAuditStatus, auditStatus)
            .eq(ObjectUtil.isNotEmpty(certificationStatus), WorkerCertificationAudit::getCertificationStatus, certificationStatus)
            .page(page);
        return PageUtils.toPage(page, WorkerCertificationAuditResDTO.class);
    }

    @Override
    @Transactional
    public void audit(Long id, Integer certificationStatus, String rejectReason) {
        UpdateWrapper<WorkerCertificationAudit> updateWrapper = new UpdateWrapper<>();
        CurrentUser currentUser = UserContext.currentUser();
        if (ObjectUtil.isNull(currentUser)) {
            throw new RuntimeException("获取当前用户失败");
        }

        Long userId = currentUser.getId();
        String currentName = currentUser.getName();
        updateWrapper.lambda().eq(WorkerCertificationAudit::getId, id)
                     .set(WorkerCertificationAudit::getCertificationStatus, certificationStatus)
                     .set(WorkerCertificationAudit::getRejectReason, rejectReason)
                     .set(WorkerCertificationAudit::getUpdateTime, LocalDateTime.now())
                     .set(WorkerCertificationAudit::getAuditorId, userId)
                     .set(WorkerCertificationAudit::getAuditorName, currentName)
                     .set(WorkerCertificationAudit::getAuditStatus, 1)
        ;

        update(updateWrapper);

        //同时更新WorkerCertification表
        WorkerCertificationAudit byId = getById(id);
        Long serveProviderId = byId.getServeProviderId();
        UpdateWrapper<WorkerCertification> updateWrapper1 = new UpdateWrapper<>();
        updateWrapper1.lambda().eq(WorkerCertification::getId, serveProviderId)
                      .set(WorkerCertification::getCertificationStatus, certificationStatus)
                      .set(WorkerCertification::getUpdateTime, LocalDateTime.now());
        workerCertificationMapper.update(null, updateWrapper1);
    }
}
